//
//  YSAlertBase.swift
//  SmartCar
//
//  Created by y s on 2022/10/19.
//

import UIKit

@objc public enum SAlertShow:Int{
    case none
    case fadeIn
    case slideInToCenter
    case slideInFormTop
    case slideInFromLeft
}

@objc public enum SAlertHide:Int{
    case none
    case fadeOut
    case slideOutToCenter
    case slideOutToRight
}

public typealias AlertLinkBlock = (String) -> ()
public typealias AlertCloseBlock = (Int) -> ()
public typealias AlertSureBlock = () -> ()
public typealias AlertHideBlock = () -> ()

@objcMembers open class YSAlertBase:UIView
{
    public var showType:SAlertShow = .none
    public var hideType:SAlertHide = .none
    public var backgroundOpacity:CGFloat = 0.5
    public var alertViewBackColor = UIColor.white
    
    // typedef void(^ChangeBlock)(void);
    public var onLoginBlock:(()->Void)?
    
    public var cancelBlock:(()->Void)?
    public var closeBlock:AlertCloseBlock?
    public var sureBlock:AlertSureBlock?
    public var hideBlock:AlertHideBlock?
    public var linkBlock:AlertLinkBlock?
    public var outsideClose:Bool = true
    public var target:UIView?
    
    public lazy var backgroundView:UIView = {
        let result = UIView()
        result.backgroundColor = SUIUtil.colorOf(0x0, alpha: backgroundOpacity)
        result.frame = CGRect(x:0,y:0,width:ScreenWidth,height:ScreenHeight)
        result.s_clickBlock({[weak self] (tap) in
            if self != nil{
                if !self!.outsideClose{
                    return
                }
                self!.hide()
                if self!.closeBlock != nil{
                    self!.closeBlock!(0)
                }
            }
        })
        result.isUserInteractionEnabled=false
        return result
    }()
    
    public lazy var alertView:UIView = {
        let result = UIView()
        return result
    }()
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override init(frame:CGRect)
    {
        super.init(frame: frame)
    }
    
    open func setupUI()
    {
        let window = UIApplication.shared.keyWindow!
        for view in window.subviews{
            if view is YSAlertBase{
                view.removeFromSuperview()
            }
        }
        reset()
    }
    
    dynamic open func reset(){
        backgroundView.alpha = backgroundOpacity
        alertView.backgroundColor = alertViewBackColor
        alertView.alpha=1
    }
    
    // 淡入
    dynamic open func fadeIn()
    {
        backgroundView.alpha = 0
        alertView.alpha=0
        UIView.animate(withDuration: 0.3) {[weak self] in
            if let opacity = self?.backgroundOpacity{
                self?.backgroundView.alpha=opacity
            }
            self?.alertView.alpha = 1
            self?.didShow()
        }
    }
    // 从顶部掉落
    dynamic open func slideInFromTop()
    {
        let frame = alertView.frame;
        alertView.frame=CGRect(x:frame.origin.x,
                               y:-frame.size.height,
                               width:frame.size.width,
                               height:frame.size.height);
        UIView.animate(withDuration:0.5, animations:{[weak self] in
            self?.alertView.frame = frame
        },completion: {[weak self] _ in
            self?.didShow()
        })
    }
    // 从中心点滑入
    dynamic open func slideInToCenter()
    {
        alertView.transform = CGAffineTransform.identity.concatenating(CGAffineTransform(scaleX:0.1,y:0.1))
        alertView.alpha = 0.0;
        UIView.animate(withDuration:0.3, animations:{[weak self] in
            self?.alertView.transform = CGAffineTransform.identity.concatenating(CGAffineTransform(scaleX:1.0,y:1.0))
            self?.alertView.alpha = 1
            },completion:{[weak self] _ in
                UIView.animate(withDuration: 0.2, animations: {
                    if let temp = self?.backgroundView.center{
                        self?.alertView.center = temp
                    }
                },completion: {[weak self] _ in
                    self?.didShow()
                })
        })
    }
    // 从左侧滑入
    dynamic open func slideInFromLeft()
    {
        var frame = alertView.frame;
        frame.origin.x = -alertView.frame.size.width;
        alertView.frame = frame;
        UIView.animate(withDuration:0.3, animations:{[weak self] in
            var frame = self?.alertView.frame
            frame?.origin.x = 0.0;
            self?.alertView.frame = frame!;
            },completion:{[weak self] _ in
                UIView.animate(withDuration: 0.2, animations: {
                    self?.alertView.center = (self?.backgroundView.center)!
                },completion: {[weak self] _ in
                    self?.didShow()
                })
        })
    }
    // 淡出
    dynamic open func fadeOut()
    {
        fadeOutWithDuration(0.3)
    }
    
    dynamic open func fadeOutWithDuration(_ duration:TimeInterval)
    {
        UIView.animate(withDuration:duration, animations:{[weak self] in
            self?.backgroundView.alpha = 0
            self?.alertView.alpha = 0
            },completion:{[weak self] _ in
                if self?.superview != nil{
                    self?.removeFromSuperview()
                    if self?.hideBlock != nil{
                        self?.hideBlock!()
                    }
                    self?.hideDone()
                }
        })
    }
    // 从中心点滑出
    dynamic open func slideOutToCenter()
    {
        UIView.animate(withDuration:0.3, animations:{[weak self] in
            self?.alertView.transform = CGAffineTransform.identity.concatenating(CGAffineTransform(scaleX:0.1,y:0.1))
            self?.alertView.alpha = 0
            },completion:{[weak self] _ in
                self?.fadeOut()
        })
    }
    // 滑出到右侧
    dynamic open func slideOutToRight()
    {
        var frame = alertView.frame;
        frame.origin.x = ScreenWidth;
        UIView.animate(withDuration:0.3, animations:{[weak self] in
            self?.alertView.frame = frame
            self?.alertView.alpha = 0
            },completion:{[weak self] _ in
                self?.fadeOut()
        })
    }
    
    dynamic public func show()
    {
        DispatchQueue.safeMainAsync {
            self.setupUI()
            if self.showType == .fadeIn{
                self.fadeIn()
                return
            }
            if self.showType == .slideInFormTop{
                self.slideInFromTop()
                return
            }
            if self.showType == .slideInToCenter{
                self.slideInToCenter()
                return
            }
            if self.showType == .slideInFromLeft{
                self.slideInFromLeft()
                return
            }
            self.didShow()
        }
    }
    
    dynamic open func willHide(){
    }
    
    dynamic open func hide()
    {
        willHide()
        DispatchQueue.safeMainAsync {
            if self.hideType == .none{
                if self.superview != nil{
                    self.removeFromSuperview()
                    if self.hideBlock != nil{
                        self.hideBlock!()
                    }
                    self.hideDone()
                }
                return
            }
            if self.hideType == .fadeOut{
                self.fadeOut()
                return
            }
            if self.hideType == .slideOutToCenter{
                self.slideOutToCenter()
                return
            }
            if self.hideType == .slideOutToRight{
                self.slideOutToRight()
                return
            }
        }
    }
    // 显示完成
    dynamic open func didShow()
    {
        backgroundView.isUserInteractionEnabled=true
    }
    // 关闭完成
    dynamic open func hideDone(){
        
    }
}
